
use crate::port::{portb};
pub use avr_hal_generic::pwm::*;

avr_hal_generic::impl_pwm! {
    /// Use `TC1` for PWM (pins `PB5`, `PB6`, `PB7`)
    ///
    /// # Example
    /// ```
    /// let mut portb = dp.PORTB.split();
    /// let mut timer1 = Timer1Pwm::new(dp.TC1, pwm::Prescaler::Prescale64);
    ///
    /// let mut pb5 = portb.pb5.into_output(&mut portb.ddr).into_pwm(&mut timer1);
    /// let mut pb6 = portb.pb6.into_output(&mut portb.ddr).into_pwm(&mut timer1);
    /// let mut pb7 = portb.pb7.into_output(&mut portb.ddr).into_pwm1(&mut timer1);
    ///
    /// pb5.set_duty(128);
    /// pb5.enable();
    /// ```
    ///
    /// **Note**: For `PB7` the method is called `into_pwm1()`!
    pub struct Timer1Pwm {
        timer: crate::pac::TC1,
        init: |tim, prescaler| {
            tim.tccr1a.modify(|_, w| w.wgm1().bits(0b01));
            tim.tccr1b.modify(|_, w| {
                w.wgm1().bits(0b01);
                match prescaler {
                    Prescaler::Direct => w.cs1().val_0x01(),
                    Prescaler::Prescale8 => w.cs1().val_0x02(),
                    Prescaler::Prescale64 => w.cs1().val_0x03(),
                    Prescaler::Prescale256 => w.cs1().val_0x04(),
                    Prescaler::Prescale1024 => w.cs1().val_0x05(),
                }
            });
        },
        pins: {
            portb::PB5: {
                ocr: ocr1a,
                into_pwm: |tim| if enable {
                    tim.tccr1a.modify(|_, w| w.com1a().bits(0b10));
                } else {
                    tim.tccr1a.modify(|_, w| w.com1a().bits(0));
                },
            },
            portb::PB6: {
                ocr: ocr1b,
                into_pwm: |tim| if enable {
                    tim.tccr1a.modify(|_, w| w.com1b().bits(0b10));
                } else {
                    tim.tccr1a.modify(|_, w| w.com1b().bits(0));
                },
            },
            portb::PB7: {
                ocr: ocr1c,
                into_pwm1: |tim| if enable {
                    tim.tccr1a.modify(|_, w| w.com1c().bits(0b10));
                } else {
                    tim.tccr1a.modify(|_, w| w.com1c().bits(0));
                },
            },
        },
    }
}